package core.september.cloudbadge.vcard.example;

import java.io.*;
import java.net.*;

public class TestHTML {

	/**
	 * @param args
	 */
	private static String _URL = "http://localhost:9090/SpringMVC/welcome";
	private static String _URL_POST = "http://localhost:9090/SpringMVC/j_spring_security_check";
	private static String _URL_REFERER = "http://localhost:9090/SpringMVC/spring_security_login";
	
	private String getSID(String user, String password) {
	      URL url;
	      HttpURLConnection conn;
	      BufferedReader rd;
	      String line;
	      String result = "";
	      try {
	         url = new URL(_URL_POST);
	         conn = (HttpURLConnection) url.openConnection();
	         conn.setRequestProperty("Content-Type", 
	                 "application/x-www-form-urlencoded");
	         conn.setRequestProperty("Referer",_URL_REFERER);
	         conn.setRequestMethod("POST");
	         conn.setUseCaches (false);
	         conn.setDoInput(true);
	         conn.setDoOutput(true);
	         DataOutputStream wr = new DataOutputStream (conn.getOutputStream ());
	         String urlParameters = "j_username="+user+"&j_password="+password;
	         wr.writeBytes (urlParameters);
	         wr.flush ();
	         wr.close ();
	         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	      }
	      
	      catch(FileNotFoundException fnf) {
	    	  return fnf.getMessage().split(";")[1].split("=")[1];
	      }
	      
	      catch (Exception e) {
	         e.printStackTrace();
	      }
	      return result;
	   }
	
	public String getSID(String SID,String user, String password) {
	      URL url;
	      HttpURLConnection conn;
	      BufferedReader rd;
	      String line;
	      String result = "";
	      
	      SID = SID == null ? getSID(user, password) : SID;
	      System.out.println("SID is: "+SID);
	      try {
	         url = new URL(_URL);
	         String cookie = "JSESSIONID="+SID;
	         conn = (HttpURLConnection) url.openConnection();
	   
	         conn.setRequestProperty("Cookie", cookie);
	         conn.setRequestMethod("GET");
	         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	         //String redirected = conn.getURL().toString().equalsIgnoreCase(_URL) ? "Logged in" : "Need auth";
	         boolean authored = conn.getURL().toString().equalsIgnoreCase(url.toString());
	         
	         if(!authored) {
	        	SID = getSID(user,password);
	        	System.out.println("Try authenticating with SID: "+SID);
	        	return getSID(SID,user,password);
	         }
	         
	         else {
	        	 System.out.println("Authenticated with SID: "+SID);
	        	 return SID;
	         }
	         
	         //System.out.println("AUTH: "+redirected);
//	         while ((line = rd.readLine()) != null) {
//	            result += line;
//	         }
//	         rd.close();
	      } 
//	      
//	      catch(FileNotFoundException fnf) {
//	    	  return fnf.getMessage().split(";")[1].split("=")[1];
//	      }
//	      
	      catch (Exception e) {
	         e.printStackTrace();
	         return null;
	      }
//	      return result;
	   }

	   public static void main(String args[])
	   {
		   TestHTML c = new TestHTML();
		  
	     System.out.println(c.getSID(null, "mkyong", "123456"));
	   }
}
